<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of IndexController
 *
 * @author Beverley
 */

class IndexController extends Zend_Controller_Action {

    function indexAction() {
        $id = $this->_request->getParam('id');
        if ($id == 0){
            $productDAO = new ProductDAO();
            $productList = $productDAO->getProductList();
        }
        else {
            $product = new Product();
            $productList = $product->fetchAll("category_id = $id and status =".Constant::$STATUS_AVAILABLE);
        }        
        $paginator = Zend_Paginator::factory($productList);
        $page = $this->_getParam('page',1);
        $paginator->setItemCountPerPage(6);
        $paginator->setCurrentPageNumber($page);
        $this->view->paginator = $paginator;
    }

    function searchAction(){
        $product = new ProductDAO();
        $key = $this->_request->getParam('keyword');
        $session = new Zend_Session_Namespace('foo');
        if ($key != "")
            $session->key = $key;                            
        $row = $product->searchProduct($session->key);
        if (count($row))
        {
            $paginator = Zend_Paginator::factory($row);
            $page = $this->_getParam('page',1);
            $paginator->setItemCountPerPage(6);
            $paginator->setCurrentPageNumber($page);
            $this->view->paginator = $paginator;
        }
        else {
            echo "No product found!";
        }
    }
    function viewAction() {
        $cate = new Category();
        $this->view->cateList = $cate->fetchAll();

        $id = $this->getRequest()->getParam('id');
        $product = new Product();

        $this->view->product = $product->fetchRow("id = '$id'");
    }

    function deniedAction() {
        $cate = new Category();
        $this->view->cateList = $cate->fetchAll();
    }
    
    function loginAction() {
        $cate = new Category();
        $this->view->cateList = $cate->fetchAll();

        $user = $this->getRequest()->getPost('username');
        $pass = $this->getRequest()->getPost('password');
        $md5pass = md5($pass);

        if (isset($user) && isset($pass) && $user!="" && $pass!="")
        {
            // retrieve database from Registry
            $dbAdapter = Zend_Registry::get('db');

            // setup database adapter
            $authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter);
            $authAdapter->setTableName('account')
                        ->setIdentityColumn('username')
                        ->setCredentialColumn('password');

            $authAdapter->setIdentity($user);
            $authAdapter->setCredential($md5pass);

            $auth = Zend_Auth::getInstance();
            $result = $auth->authenticate($authAdapter);

            // returns true if the result represents a successful authentication
            if ($result->isValid())
            {
                $tblAccount = new Account();
                $select = $tblAccount->select()->where('username like ?',$user)
                                                ->where('status = ?', Constant::$STATUS_AVAILABLE);
                $accountRow = $tblAccount->fetchRow($select);

                if (!$accountRow) {
                    Zend_Auth::getInstance()->clearIdentity();
                    $messages = array('error' => array('No such username exists'));
                    $this->view->messages = $messages;
                } else {
                // store database row to auth's storage
                //where the password column has been omitted
                $auth->getStorage()
                           ->write($authAdapter->getResultRowObject(null, 'password'));
                
                $userSession = new Zend_Session_Namespace('user');
                $userSession->user_id = $accountRow->id;
                $this->_redirect(index/index);
                }
            } else {
                switch ($result->getCode())
                {
                    case Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND:
                        $messages = array('username' => array('No such username exists'));
                        break;
                    case Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID:
                        $messages = array('password' => array('Does not match current password'));
                        break;
                    default:
                        throw new Exception('Unsupported authentication failure code');
                        break;
                }
                $this->view->messages = $messages;
            }

        }

    }
    
    function logoutAction() {
        Zend_Auth::getInstance()->clearIdentity();
        $userSession = new Zend_Session_Namespace('user');
        $userSession->user_id = -1;

        $productSession = new Zend_Session_Namespace('product');
        $productSession->__unset('product');
        $this->_redirect(index/index);
    }

    function aboutAction() {
        
    }

    function contactAction() {
        
    }
}

